package com.gitee.xmhzzz.mqtt.broker.quick.start;


import com.gitee.xmhzzz.component.mqtt.broker.StandaloneMQTTBroker;
import com.gitee.xmhzzz.component.mqtt.broker.StandaloneMQTTBrokerBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadFactory;

@Slf4j
@Component
public class MqttBrokerStart implements CommandLineRunner, Runnable {


    @Override
    public void run() {
        StandaloneMQTTBroker standaloneMQTTBroker = new StandaloneMQTTBroker(new StandaloneMQTTBrokerBuilder());
        try {
            standaloneMQTTBroker.start();
        } catch (Exception e) {
            log.error("mqtt broker error msg[{}]",e.getMessage(),e);
        }
    }

    @Override
    public void run(String... args){
        ThreadFactory threadFactory = StandaloneMQTTBroker.newThreadFactory("mqtt-broker-thread", true, 1);
        threadFactory.newThread(this::run).start();
    }
}